package com.crm.util;

import java.math.BigDecimal;

public class MathEx {

	public static double mathJJCC(Double number1, Double number2, int scale, int type){
		BigDecimal b1 = new BigDecimal(Double.toString(number1));
		BigDecimal b2 = new BigDecimal(Double.toString(number2));
		
		double result = 0.00;
		
		BigDecimal cs = new BigDecimal("1");
		if(1 == type){//加
			result = b1.add(b2).divide(cs, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
		}else if(2 == type){//减
			result = b1.subtract(b2).divide(cs, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
		}else if(3 == type){//乘
			result = b1.multiply(b2).divide(cs, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
		}else if(4 == type){//除
			result = b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		return result;
	}
	
	public static int mathBatchAdd(String[] intArray){
		if(intArray == null || intArray.length == 0){
			return 0;
		}
		
		Integer result = 0; 
		for(String str : intArray){
			result = result + RequestEx.getInteger(str);
		}
		return result;
	}
	
	public static double mathBatchAdd(String[] numberArray, int scale){
		if(numberArray == null || numberArray.length == 0){
			return 0.0;
		}
		
		BigDecimal cs = new BigDecimal("1");
		
		BigDecimal result = new BigDecimal("0");; 
		for(String str : numberArray){
			result = result.add(new BigDecimal(RequestEx.getDouble(str)));
		}
		return result.divide(cs, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

}
